<template>
	<div class="monitor-1">
		<div>
			<el-date-picker @change='dateChange' v-model="createTime" type="date" :placeholder="$t('route.Select_date')" />

			<div v-if="false" class="div-handle-right">
				<el-button @click='jumpPage(0)'>{{ $t('route.Add') }}</el-button>
				<el-button>{{ $t('route.Delete') }}</el-button>
				<el-button>{{ $t('route.search') }}</el-button>
				<input :placeholder="$t('route.keyword')" />
			</div>
		</div>


		<div>
			<div>
				<div v-if="chartShow" class="chart" id="echart-1"></div>
			</div>
			<div>
				<div v-if="chartShow" class="chart" id="echart-2"></div>
			</div>
		</div>

		<div>
			<div>
				<div v-if="chartShow" class="chart" id="echart-3"></div>
			</div>
			<div>
				<div v-if="chartShow" class="chart" id="echart-4"></div>
			</div>
		</div>
	</div>
</template>


<script>
	import http from "../../js/http/http.js";
	import api from "../../js/http/api.js";
	import CommonUtils from "../../js/common/common-utils.js";
	import * as echarts from 'echarts';
	import i18n from '../../i18n/index';
	export default {
		data() {
			return {
				device: null,
				createTime: new Date(),
				chartShow: false
			};
		},
		methods: {
			getDeviceBootUpDurReport() {
				http.post(api.GET_DEVICE_BOOT_UP_DUR_REPORT, {
					deviceId: this.device.id,
					createTime: this.createTime.format('yyyy-MM-dd')
				}, data => {
					var xData = [];
					var yData = [];
					for (var item of data.data) {
						xData.push(item.time);
						yData.push(item.bootUpDur)
					}
					this.initEchart1(xData, yData);
				});
			},
			getDeviceWorkDurReport() {
				http.post(api.GET_DEVICE_WORK_DUR_REPORT, {
					deviceId: this.device.id,
					createTime: this.createTime.format('yyyy-MM-dd')
				}, data => {
					var xData = [];
					var yData = [];
					for (var item of data.data) {
						xData.push(item.time);
						yData.push(item.workDur);
					}
					this.initEchart2(xData, yData);

				});
			},

			getDeviceOfflineReport() {
				http.post(api.GET_DEVICE_OFFLINE_REPORT, {
					deviceId: this.device.id,
					createTime: this.createTime.format('yyyy-MM-dd')
				}, data => {
					var xData = [];
					var yData = [];
					for (var item of data.data) {
						xData.push(item.time);
						yData.push(item.count);
					}
					this.initEchart3(xData, yData);
				});
			},

			getDevicesSignalReport() {
				http.post(api.GET_DEVICES_SIGNAL_REPORT, {
					deviceId: 601,
					createTime: this.createTime.format('yyyy-MM-dd')
				}, data => {
					var xData = [];
					var yData = [];
					for (var item of data.data) {
						var itemAry = item.split("_")
						xData.push(itemAry[0]);
						yData.push(itemAry[1]);
					}
					this.initEchart4(xData, yData);
				});
			},


			dateChange() {
				this.getDeviceBootUpDurReport();
			},



			initEchart1(xData, yData) {
				var myChart = echarts.init(document.getElementById("echart-1"));
				var option = CommonUtils.initLineChartOption(i18n.global.t('route.Start_time_curve'), xData, yData);
				myChart.setOption(option);
			},

			initEchart2(xData, yData) {
				var myChart = echarts.init(document.getElementById("echart-2"));
				var option = CommonUtils.initLineChartOption(i18n.global.t('route.Working_time_curve'), xData, yData);
				myChart.setOption(option);
			},
			initEchart3(xData, yData) {
				var myChart = echarts.init(document.getElementById("echart-3"));
				var option = CommonUtils.initLineChartOption(i18n.global.t('route.Drop_off_frequency_curve'), xData, yData);
				myChart.setOption(option);
			},
			initEchart4(xData, yData) {
				var myChart = echarts.init(document.getElementById("echart-4"));
				var option = CommonUtils.initLineChartOption(i18n.global.t('route.signal_curve'), xData, yData);
				myChart.setOption(option);
			},


		},
		created() {

		},
		mounted() {
			this.chartShow = true;
			this.device = JSON.parse(this.$route.params.device);
			this.getDeviceBootUpDurReport();
			this.getDeviceWorkDurReport();
			this.getDeviceOfflineReport();
			this.getDevicesSignalReport();
		},

		destroyed() {
			this.chartShow = false;
			console.log("======destroyed====")
		}
	};
</script>

<style lang="scss" scoped>
	.monitor-1 {
		>div:nth-of-type(1) {
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-top: 20px;
			background-color: #ffffff;
			padding: 20px;
			border-radius: 5px;

			.div-handle-right {
				flex: 1;
				flex-direction: row-reverse;
				text-align: right;
				display: flex;
				align-items: center;

				>input:nth-of-type(1) {
					border: 1px solid #eeeeee;
					font-size: 15px;
					width: 150px;
					padding: 5px 8px;
					outline: none;
				}

				>button {
					color: #A866FF;
					margin-left: 10px;
					padding: 4px 20px;
				}
			}
		}

		>div:nth-of-type(2),
		>div:nth-of-type(3) {
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-top: 5px;

			>div {
				background-color: #ffffff;
				padding: 20px;
				border-radius: 5px;
				flex: 1;
			}

			>div:nth-of-type(1) {
				margin-right: 10px;
			}

			.chart {
				height: 250px;
				width: 100%;

			}

		}


	}
</style>
